package servlet;

import com.alibaba.fastjson.JSON;
import entity.PageResult;
import entity.R;
import entity.StudentVo;
import entity.student;
import mapper.StudentMapper;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import service.StudentService;
import service.impl.StudentServiceImpl;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static net.sf.jsqlparser.parser.feature.Feature.delete;
import static net.sf.jsqlparser.parser.feature.Feature.update;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService;

    @Override
    public void init() throws ServletException {
        studentService =new StudentServiceImpl();
    }

    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //设置响应内容及字符集编码
        resp.setContentType("text/html;charset=utf8");
        //得到cmd查询参数的值
        String cmd = req.getParameter("cmd");
        //根据不同的cmd值查询不同的方法
        //判断是否为空
        if (StringUtils.isNoneBlank(cmd)){
            //再判断方法名
            if ("findByPage".equals(cmd)){
                findByPage(req,resp);
                //列表查询
            }else if ("list".equals(cmd)){
                list(req,resp);
            }else if("search".equals(cmd)){
                //条件查询带分页
                search(req,resp);
            }else if("add".equals(cmd)){
                //添加学生
                add(req,resp);
            }else if("update".equals(cmd)){
                //修改学生
                update(req,resp);
            }else if ("delete".equals(cmd)){
                delete(req,resp);
            }
        }
    }
    //删除学生
    private void delete(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        //得到要删除的学号
        String sids = req.getParameter("sids");
        //将学生编号放入集合
        List<String> ids = new ArrayList<>();
        //拆分学号
        String[] split = sids.split(",");
        if (split.length==0){
            ids.add(sids);
        }else {
            ids= Arrays.asList(split);
        }
        studentService.delete(ids);
        R r = R.ok().put("message", "删除成功");
        resp.getWriter().println(JSON.toJSONString(r));
        resp.getWriter().flush();
    }

    //修改学生
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //得到传入的数据
        String stud = req.getParameter("stud");
//        转化为学生对象
        student student = JSON.parseObject(stud, student.class);
        //保存学生对象
        studentService.update(student);
        R r = R.ok().put("message", "修改成功");
        resp.getWriter().println(JSON.toJSONString(r));
        resp.getWriter().flush();
    }
    //添加学生
    private void add(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        //得到传入的数据
        String stud = req.getParameter("stud");
//        转化为学生对象
        student student = JSON.parseObject(stud, student.class);
        //保存学生对象
        studentService.save(student);
        R r = R.ok().put("message", "添加成功");
        resp.getWriter().println(JSON.toJSONString(r));
        resp.getWriter().flush();
    }

    private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //得到分页参数
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        int page = new Integer(pageStr);
        int pageSize=new Integer(pageSizeStr);
        //得到分页查询对象
        String s = req.getParameter("stud");
//        转化成java对象
        StudentVo studentVo = JSON.parseObject(s, StudentVo.class);
        PageResult<student> pr= studentService.search(page,pageSize,studentVo);
        //将Pr对象放入R对象结果集
        R r = R.ok().put("pr", pr);
        //将结果集对象转化成为字符串
        String s1 = JSON.toJSONString(r);
        //输出到页面
        resp.getWriter().println(s1);
        resp.getWriter().flush();
    }

    //列表查询
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //查询所有学生列表
        List<entity.student> students= studentService.findALL();
        //将学生对象放入R对象
        R data = R.ok().put("list", students);
        //R对象转化为JSON字符串
        String s = JSON.toJSONString(data);
        //输出到页面
        resp.getWriter().println(s);
        //刷新页面
        resp.getWriter().flush();


    }

    //分页查询
    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
    //得到分页参数
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        int page = new Integer(pageStr);
        int pageSize=new Integer(pageSizeStr);
        //查询当前页的数据
        PageResult<student> pr= studentService.findByPage(page,pageSize);
        //将Pr对象放入R对象结果集
        R r = R.ok().put("pr", pr);
        //将结果集对象转化成为字符串
        String s = JSON.toJSONString(r);
        //输出到页面
        resp.getWriter().println(s);
        resp.getWriter().flush();
    }

}
